package com.jackrain.nea.fc.ps.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.ShopQueryCmd;
import com.jackrain.nea.fc.ps.mapper.ShopQueryMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;

/**
 * 商标准品档案 服务接口service层
 *
 * @author zhang.zz
 * @date 2018/09/28
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ps")
public class ShopQueryCmdImpl extends CommandAdapter implements ShopQueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ShopQueryMapper shopQueryMapper = ApplicationContextHandle.getBean(ShopQueryMapper.class);
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ShopQueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        Object[] idsArr = param.getJSONArray("IDS").toArray();
        //存放封装的父子数据
        JSONArray jsonArrayobj = new JSONArray();
        for (Object ids : idsArr) {
            HashMap pro = shopQueryMapper.selectShopPro(TypeUtils.castToLong(ids));
            Long id = TypeUtils.castToLong(pro.get("ID"));
            //图片 写死
            pro.put("IMAGE", "[{\"NAME\":'',\"URL\":\"https://img.alicdn.com/imgextra/i1/3528106876/O1CN0120fFoUHOIgO6IZo_!!3528106876.png\"}]");

//            String image = TypeUtils.castToString(pro.get("IMAGE"));
//            JSONArray json = JSONArray.parseArray(image);
//            if (null != image) {
//                String imageArray = "";
//                if (json.size() > 5) {
//                    imageArray = json.subList(0, 5).toString();
//                } else {
//                    imageArray = json.toString();
//                }
//                pro.put("IMAGE", imageArray);
//            } else {
//                pro.put("IMAGE", "[{\"NAME\":'',\"URL\":\"http://profcweb.oss-cn-hangzhou.aliyuncs.com/PS_C_PRO/15068739/1b927068-b9f7-421e-9e50-cb5db2d53195.png\"}]");
//            }
            //查询子表
            List<HashMap> sku = shopQueryMapper.selectShopSku(id);
            //存放单ids父子数据
            JSONObject jsonObject = new JSONObject();

            JSONObject jsonObjectPro = new JSONObject(pro);
            JSONArray jsonArraySku = new JSONArray();
            for(HashMap hashMap :sku){
                JSONObject jsonObjectSku = new JSONObject(hashMap);
                jsonArraySku.add(jsonObjectSku);
            }
            jsonObject.put("jsonPro",jsonObjectPro);
            jsonObject.put("jsonSku",jsonArraySku);
            JSONObject date = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArrayobj.add(date);
        }

        vh.put("data", jsonArrayobj);
        return vh;
    }
}

